<?php
        
// Authenticate :
require("class.AuthenticationManager.php");
require("class.CommandMenu.php");
if (!$authenticationManager->isLoggedIn()) 
{
	Header("Location: login.php?redirect=$_SERVER[PHP_SELF]");
	exit;
}

// Connect to database :
$dbh         = dbConnect();
$contextUser = strtolower($_SESSION['contextUser']);

if (empty($contextUser))
{
	errorPage("Could not determine the context user");
}

// Load local vars from superglobals :
$year        = $_REQUEST["year"];
$month       = $_REQUEST["month"];
$day         = $_REQUEST["day"];
$destination = $_REQUEST["destination"];
$proj_id     = isset($_REQUEST["proj_id"]) ? $_REQUEST["proj_id"]: 0;
$task_id     = isset($_REQUEST["task_id"]) ? $_REQUEST["task_id"]: 0;
$client_id   = isset($_REQUEST["client_id"]) ? $_REQUEST["client_id"]: 0;

// Get todays values :
$today      = time();
$todayYear  = date("Y", $today);
$todayMonth = date("n", $today);
$todayDay   = date("j", $today);

// Check that the client id is valid:
if ($client_id == 0 || empty($client_id))
	$client_id = getFirstClient();

// Check that project id is valid:
if ($proj_id == 0)
	$task_id = 0;

// Calculate tomorrow and yesterday for "prev" & "next" buttons:
$yesterday = mktime(0,0,0,$month,$day,$year) - 24*60*60;
$tomorrow  = mktime(0,0,0,$month,$day,$year) + 24*60*60;

function getDailyTimes($month, $day, $year, $id, $proj_id) {
	include("table_names.inc");
	list($qhq, $numq) = dbQuery("select timeformat from $CONFIG_TABLE where config_set_id = '1'");
	$configData = dbResult($qhq);
		
	$query = "select date_format(start_time,'%d') as day_of_month, trans_num, ";
	
	if ($configData["timeformat"] == "12")
		$query .= "date_format(end_time, '%l:%i%p') as endd, date_format(start_time, '%l:%i%p') as start, ";
	else
		$query .= "date_format(end_time, '%k:%i') as endd, date_format(start_time, '%k:%i') as start, ";
		$query .= "unix_timestamp(end_time) - unix_timestamp(start_time) as diff_sec, " .
							"unix_timestamp(start_time) as start_time, " .
							"unix_timestamp(end_time) as end_time, " .
							"end_time as end_time_str, " .
							"start_time as start_time_str, ".
							"$PROJECT_TABLE.title as project_title, " .
							"$TASK_TABLE.name, $TIMES_TABLE.proj_id, $TIMES_TABLE.task_id " .
							"FROM $TIMES_TABLE, $TASK_TABLE, $PROJECT_TABLE " .
							"WHERE $TASK_TABLE.proj_id=$PROJECT_TABLE.proj_id AND " .
							"uid='$id' AND ";
	
	$query .= "$TASK_TABLE.task_id = $TIMES_TABLE.task_id AND " .
						"((start_time >= '$year-$month-$day 00:00:00' AND start_time <= '$year-$month-$day 23:59:59') " .
						" OR (end_time >= '$year-$month-$day 00:00:00' AND end_time <= '$year-$month-$day 23:59:59') " .
						" OR (start_time < '$year-$month-$day 00:00:00' AND end_time > '$year-$month-$day 23:59:59')) " .
						" order by day_of_month, start_time";

	list($my_qh, $num) = dbQuery($query);
		return array($num, $my_qh);
}


//include date input classes
include "form_input.inc";

list($num, $qh) = getDailyTimes($month, $day, $year, $contextUser, $proj_id);













?>
<html>
    <head>
    <title>
	    Update timesheet for 
		<?php echo $contextUser; ?>
	</title>
    <?php 
    include("header.inc");
    include("client_proj_task_javascript.inc");
	
    ?>
    <script language="Javascript">
    
    function resizePopupWindow() {
    	//now resize the window
    	var outerTable = document.getElementById('outer_table');
    	var newWidth   = outerTable.offsetWidth + window.outerWidth - window.innerWidth;
    	var newHeight  = outerTable.offsetHeight + window.outerHeight - window.innerHeight;
    	window.resizeTo(newWidth, newHeight);
    }
    </script>
    </HEAD>
    <body style="margin: 0; padding: 0;" class="face_padding_cell" <?php include ("body.inc"); ?> onload="doOnLoad();">
    	<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" id="outer_table">	
    		<tr>
    		    <td width="100%" class="face_padding_cell">				
    				<table width="100%" border="0">
    					<tr>
    						<td align="left" nowrap class="outer_table_heading" nowrap>
    							Client / Project / Task
    						</td>
    						<td align="right" nowrap class="outer_table_heading">
    							<?php echo strftime("%A %B %d, %Y", mktime(0,0,0,$month, $day, $year)); ?>
    						</td>												
    					</tr>
    				</table>			 
    	            <table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" class="outer_table">
    	            	<form action="action.php" method="post"         name ="addForm" id="theForm">
    	            	    <input type="hidden" name="year"            value="<?php echo $year; ?>">
    	            	    <input type="hidden" name="month"           value="<?php echo $month; ?>">
    	            	    <input type="hidden" name="day"             value="<?php echo $day;   ?>">
    	            	    <input type="hidden" id  ="client_id"       value="<?php echo $client_id; ?>" name ="client_id" >
    	            	    <input type="hidden" id  ="proj_id"         value="<?php echo $proj_id;   ?>" name ="proj_id"   >
    	            	    <input type="hidden" id  ="task_id"         value="<?php echo $task_id;   ?>" name ="task_id"   > 
    	            	    <input type="hidden" name="fromPopupWindow" value="true">
    	            	    <input type="hidden" name="origin"          value="<?php echo $_SERVER["PHP_SELF"]; ?>">
    	            	    <input type="hidden" name="destination"     value="<?php echo $destination; ?>">
    	            	    <tr>
    	            	    	<td>				
    	            	    		<table width="100%" border="0" cellpadding="1" cellspacing="2" class="table_body">			
    	            	    			<tr>
    	            	    				<td>
    	            	    					<table width="100%" border="0">
    	            	    						
													<tr>
    	            	    							<td align="left" width="100%" nowrap>
    	            	    									<table width="100%" border="0" cellspacing="0" cellpadding="0">
    	            	    										<tr>
    	            	    											<td>
																		    <table width="50"><tr><td>Client:</td></tr></table></td>
    	            	    											<td width="100%">
    	            	    												<select id="clientSelect" name="clientSelect"  onChange="onChangeClientSelect();" style="width: 100%;" />
    	            	    											</td>																										
    	            	    										</tr>
    	            	    									</table>
    	            	    							</td>									
    	            	    						</tr>
                                                    <!--		-->										
    	            	    						<tr>
    	            	    							<td align="left" width="100%" nowrap>
    	            	    									<table width="100%" border="0" cellspacing="0" cellpadding="0">
    	            	    										<tr>
    	            	    											<td><table width="50"><tr><td>Project:</td></tr></table></td>
    	            	    											<td width="100%">
    	            	    												<select id="projectSelect" name="projectSelect" onChange="onChangeProjectSelect();" style="width: 100%;" />
    	            	    											</td>
    	            	    										</tr>
    	            	    									</table>
    	            	    								</td>									
    	            	    						</tr>																		
    	            	    						<tr>
    	            	    							<td align="left" width="100%">
    	            	    									<table width="100%" border="0" cellspacing="0" cellpadding="0">
    	            	    										<tr>
    	            	    											<td><table width="50"><tr><td>Task:</td></tr></table></td>
    	            	    											<td width="100%">
    	            	    												<select id="taskSelect" name="taskSelect" onChange="onChangeTaskSelect();" style="width: 100%;" />
    	            	    											</td>
    	            	    										</tr>
    	            	    									</table>
    	            	    							</td>									
    	            	    						</tr>																										
    	            	    						<tr>
    	            	    							<td>
    	            	    								<table width="100%" border="0">
																<tr>								
    	            	    										<td align="center">
    	            	    											<table width="350" border="0" class="clock_on_box">
    	            	    												<tr>
    	            	    													<td>
    	            	    														<img src="images/clock-green-sml.gif" border="0">
    	            	    													</td>
                                                                                <td align="left" class="clock_on_text">
    	            	    														Time available for this task:
    	            	    													</td>																			
    	            	    													<td valign="middle">
																					<select name="dayInput" id="dayInput" size="1">
                                                                                        <option>0.25</option>
                                                                                        <option>0.50</option>
                                                                                        <option>0.75</option>
                                                                                        <option selected value="">1
                                                                                    </select>
    	            	    													</td>
																				<td align="left" class="clock_on_text">
    	            	    														day
    	            	    													</td>
    	            	    												</tr>
    	            	    											</table>
    	            	    										</td>
    	            	    									</tr>
    	            	    									<tr>
    	            	    										<td align="center">
    	            	    											<input type="button" value="Validate" name="submitButton" id="submitButton" onClick="onSubmit();">
    	            	    										</td>
    	            	    									</tr>																	
    	            	    								</table>
    	            	    							</td>
    	            	    						</tr>
    	            	    					</table>
    	            	    				</td>					
    	            	    			</tr>							
    	            	    		</table>
    	            	    	</td>
    	            	    </tr>
    	            	</form>
    	            </table>
    				
    				
    			</td>			
    		</tr>
    	</table>
    </BODY>
</HTML>
